package com.xiaoy.core.database.form.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Results;

import com.google.common.collect.Lists;
import com.xiaoy.core.database.form.entity.Field;
import com.xiaoy.core.database.form.entity.View;
import com.xiaoy.core.database.form.server.FieldService;
import com.xiaoy.core.database.form.server.ViewService;

@Namespace("/view")
@Results( {
})
public class ViewAction  extends FetterAction {
	
	@Resource
	private ViewService viewService;
	@Resource
	private FieldService fieldService;
	
	public String settings_fields () {
		
		String[] tables = getParameters("tables");
		
		String[] tablenames = getParameters("tablename");
		String[] fieldNames = getParameters("fieldName");
		String[] labels = getParameters("label");
		String[] isHiddens = getParameters("isHidden");
		String[] isSearchs = getParameters("isSearch");
		String[] searchTypes = getParameters("searchType");
		
		String[] isOrders = getParameters("isOrder");
		String[] orderTypes = getParameters("orderType");
		String[] isGroups = getParameters("isGroup");
		
		List<Field> fields = Lists.newArrayList();
		
		String sql = "select $fields from $tablenames $where $group $order ";
		String fieldSql = "";
		String tablenameSql = "";
		String orderSql = "";
		String groupSql = "";
		
		StringBuilder whereSql = new StringBuilder("where 1=1 ");;
		String tempTablename = "";
		
		for (int j =0;j<tables.length;j++) {
			
			int i = Integer.parseInt(tables[j]);
			
			String tablename = tablenames[i];
			String name = fieldNames[i];
			String label = labels[i];
			String isHidden = isHiddens[i];
			String isSearch = isSearchs[i];
			String searchType = searchTypes[i];
			
			String isOrder = isOrders[i];
			String orderType = orderTypes[i];
			String isGroup = isGroups[i];
			
			if("1".equals(isOrder)) {
				orderSql += name + " " + orderType +"," ;
			}
			
			if ("1".equals(isGroup)) {
				groupSql += name+",";
			}
			
			// 联表
			if (!tempTablename.equals(tablename)) {
				
				if ("".equals(tempTablename)) {
					tablenameSql += tablename ;
				}else {
					tablenameSql += " left join "+tablename+" on "+tempTablename+".id = " ; 
				}
				tempTablename =tablename;
			}
			
			// 字段
			fieldSql += tablename+"."+name+",";
			
			// 查询
			if ("1".equals(isSearch)) {
				whereSql.append(" and "+name);
				if ("eq".equals(searchType)) {			//=
					whereSql.append(" = ");
				}else if ("like".equals(searchType)) {	//like
					whereSql.append(" like ");
				}else if ("le".equals(searchType)) {	//>=
					whereSql.append(" >= ");
				}else if ("lt".equals(searchType)) {	//>
					whereSql.append(" > ");
				}else if ("gt".equals(searchType)) {	//<
					whereSql.append(" < ");
				}else if ("ge".equals(searchType)) {	//<=
					whereSql.append(" <= ");
				}
				whereSql.append(" #"+name+" ");
			}
			
			
			Field field = new Field(tablename,name,label);
			field.setIsSearch(isSearch);
			field.setIsHidden(isHidden);
			field.setSearchType(searchType);
			fields.add(field);
//			fetterService.save(fetter);
		}
		
		if (orderSql.length()>1) {
			sql = sql.replace("$order"," order by " + orderSql.substring(0, orderSql.length()-1));
		}else {
			sql = sql.replace("$order", "");
		}
		
		if (groupSql.length()>1) {
			sql = sql.replace("$group", "group by "+groupSql.substring(0,groupSql.length()-1));
		}else {
			sql = sql.replace("$group", "");
		}
		
		sql = sql.replace("$where", whereSql).replace("$tablenames", tablenameSql).replace("$fields", fieldSql.substring(0, fieldSql.length()-1));
		
		
		setAttribute("sql", sql);
		setSessionAttribute("fields", fields);
		return "settingsFields";
	}
	
	public String setted () {
		List<Field> fields = (List<Field>) getSessionAttribute("fields");
		String sql = getParameter("sql");
		String name = getParameter("name");
		View view = new View(name,sql);
		
		viewService.save(view);
		
		for (Field f :fields) {
			f.setBusinessId(view.getId());
			fieldService.save(f);
		}
		return "setted";
	}
	 
	
	public String list () {
		
		setAttribute("pageBean",viewService.filterPage(initPageWrapper()));
		
		return "list";
	}
}


